﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WIA;
using System.IO;

namespace TwainTest
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                DeviceManager manager = new DeviceManagerClass();
                Device device = null;
                foreach (DeviceInfo info in manager.DeviceInfos)
                {
                    if (info.Type != WiaDeviceType.ScannerDeviceType)
                        continue;
                    device = info.Connect();
                    break;
                }
                Item item = device.Items[1];
                CommonDialogClass cdc = new WIA.CommonDialogClass();
                ImageFile imageFile = cdc.ShowAcquireImage(WIA.WiaDeviceType.ScannerDeviceType,
                                                WIA.WiaImageIntent.TextIntent,
                                                WIA.WiaImageBias.MaximizeQuality, "{00000000-0000-0000-0000-000000000000}", true, true, false);
                imageFile = cdc.ShowAcquireImage(WIA.WiaDeviceType.ScannerDeviceType,
                                                WIA.WiaImageIntent.TextIntent,
                                                WIA.WiaImageBias.MaximizeQuality, "{B96B3CB0-0728-11D3-9D7B-0000F81EF32E}", true, true, false);
                if (imageFile != null)
                {
                    var buffer = imageFile.FileData.get_BinaryData() as byte[];
                    using (MemoryStream ms = new MemoryStream())
                    {
                        ms.Write(buffer, 0, buffer.Length);
                        try
                        {
                            //SealPicture.Image = Image.FromStream(ms);
                        }
                        catch { }
                    }
                }
                //ImageBuild();
            }
            catch
            {
                //MessageBox.Show("请检查扫描仪是否连接正确！", "提示...", MessageBoxButtons.OK,
                //MessageBoxIcon.Stop);
            }
        }
    }
}
